package com.rory.tiny.modules.pms.task;

import com.rory.tiny.common.enums.RedisKeyPrefixEnum;
import com.rory.tiny.common.service.RedisService;
import com.rory.tiny.modules.pms.dto.request.Lv2CodeRequestDTO;
import com.rory.tiny.modules.pms.model.PmsLv2Code;
import com.rory.tiny.modules.pms.model.PmsProvinceSelect;
import com.rory.tiny.modules.pms.service.PmsLv2CodeService;
import com.rory.tiny.modules.pms.service.PmsProvinceSelectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 字段任务服务
 *
 * @author zhangzedi
 * @date 2022/12/28 22:53
 **/
@Component
@Slf4j
public class ScheduledService {

	@Resource
	private PmsProvinceSelectService provinceSelectService;

	@Resource
	private RedisService redisService;

	@Resource
	private PmsLv2CodeService pmsLv2CodeService;

	/**
	 * 定时更新省份点击数据,每隔5分钟执行一次：
	 *
	 * @param
	 * @return
	 * @author zhangzedi
	 * @date 2022/12/28 22:54
	 */
	@Scheduled(cron = "0 */2 * * * ?")
	public void updateProvinceSelect() {
		//查询所有的省份代码
		Lv2CodeRequestDTO lv2CodeRequestDTO = new Lv2CodeRequestDTO();
		lv2CodeRequestDTO.setType("province_code");
		List<PmsLv2Code> pmsLv2CodeList = pmsLv2CodeService.queryAreaCode(lv2CodeRequestDTO);
		pmsLv2CodeList.forEach((item -> {
			Object numObj = redisService.get(RedisKeyPrefixEnum.AREA_SELECT_NUM.getPrefix() + item.getValue());
			if (Objects.nonNull(numObj)) {
				PmsProvinceSelect model = new PmsProvinceSelect();
				model.setAreaCode(item.getValue());
				model.setSelectNum((Integer) numObj);
				provinceSelectService.insert(model);
				log.info(item.getValue() + ":自动更新数据成功");
			}
		}));

	}
}
